package com.junjie.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junjie.entity.RDevice;
import com.junjie.services.RDeviceService;
import com.junjie.utils.CommonParams;
import com.junjie.utils.CommonResult;
import com.junjie.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/rDevice")
@ResponseBody
public class RDeviceController {

    @Autowired
    private RDeviceService rDeviceService;

    //通用查询接口
    @RequestMapping(value = "/getRDevices", method = RequestMethod.POST)
    public CommonResult getRDevices(@RequestBody JSONObject json, HttpServletRequest request) {
        RDevice rDevice = new RDevice();
        String token = request.getHeader("Authorization");

        //获取当前用户房间的rid
        String room_id = JWTUtils.getTokenStringValue(token, "room_id");
        //获取公寓的apt_id
        Integer apt_id = Integer.parseInt(JWTUtils.getTokenStringValue(token, "apt_id"));
        //获取角色id
        int rule_id = Integer.parseInt(JWTUtils.getTokenStringValue(token, "role_id"));

        rDevice.setId(json.getInteger("id"));
        String r = json.getString("rid");
        Integer a = json.getInteger("apt_id");

        if((r == null || r.equals("")) && a == null) {
            //如果是管理员
            if(rule_id == 2) {
                rDevice.setApt_id(apt_id);
            //如果是普通员工，只会查询当前员工房间的所哟设备
            } if(rule_id == 3) {
                rDevice.setApt_id(apt_id);
                rDevice.setRId(room_id);
            }
        } else {
            rDevice.setRId(r);
            rDevice.setApt_id(a);
        }
        rDevice.setName(json.getString("name"));
        rDevice.setPrice(json.getBigDecimal("price"));
        rDevice.setDStatus(json.getInteger("dstatus"));
        rDevice.setReport(json.getInteger("report"));
        rDevice.setStatus(1);
        PageHelper.startPage(json.getInteger("pageNum"), json.getInteger("pageSize"));
        List<RDevice> rDevices = rDeviceService.getRDevices(rDevice);
        PageInfo<RDevice> pageInfo = new PageInfo<>(rDevices);
        return new CommonResult(CommonParams.SUCCESS, "信息查询成功", pageInfo);
    }

    //根据主键 id 来进行查询
    @RequestMapping(value = "/getRDeviceByIdRIdStatus", method = RequestMethod.GET)
    public CommonResult getRDeviceByIdRIdStatus(@RequestParam("id") Integer id, @RequestParam("rId") Integer rId, @RequestParam("status") Integer status) {
        return new CommonResult(CommonParams.SUCCESS, "信息查询成功", rDeviceService.getRDeviceByIdRIdStatus(id, rId, status));
    }

    //插入一条设备信息的方法
    @RequestMapping(value = "/insertRDevice", method = RequestMethod.POST)
    public CommonResult insertRDevice(@RequestBody JSONObject json) {
        RDevice rDevice = new RDevice();
        rDevice.setRId(json.getString("rid"));
        rDevice.setApt_id(json.getInteger("apt_id"));
        rDevice.setName(json.getString("name"));
        rDevice.setPrice(json.getBigDecimal("price"));
        rDevice.setDStatus(json.getInteger("dstatus"));
        rDevice.setReport(10);// report 无报修信息
        rDevice.setStatus(1);
        return rDeviceService.insertRDevice(rDevice);
    }

    //修改一条设备的信息
    @RequestMapping(value = "/updateRDeviceById", method = RequestMethod.POST)
    public CommonResult updateRDeviceById(@RequestBody JSONObject json) {
        RDevice rDevice = new RDevice();
        rDevice.setId(json.getInteger("id"));
        rDevice.setRId(json.getString("rid"));
        rDevice.setApt_id(json.getInteger("apt_id"));
        rDevice.setName(json.getString("name"));
        rDevice.setPrice(json.getBigDecimal("price"));
        rDevice.setDStatus(json.getInteger("dstatus"));
        rDevice.setReport(json.getInteger("report"));
        return rDeviceService.updateRDeviceById(rDevice);
    }

    //删除一条数据的方法
    @RequestMapping(value = "/deleteRDevice/{id}", method = RequestMethod.POST)
    public CommonResult deleteRDevice(@PathVariable("id") Integer id) {
        return rDeviceService.deleteRDevice(id);
    }
}
